/*
 * checkout.java
 *
 * Created on March 26, 2007, 7:11 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package edu.byu.isys413.actions;

import javax.servlet.http.*;
import edu.byu.isys413.group1E.data.*;
import edu.byu.isys413.group1E.handlers.*;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**This action class adds a Photo revenue source to the transaction
 *
 * @author Kendall Frazier
 */
public class addphoto implements edu.byu.isys413.web.Action {
    
    /**
     * Creates a new instance of checkout
     */
    public addphoto() {
    }
  
     /** 
     *Processes method adds a new Transactionline with a print RevenueSource to the Transaction.  It takes in the parameters
     *from the previous webpage.  When it is done, it calls cart2.jsp to display the current shopping cart. 
     *
     *@param HttpServletRequest
     *@param HttpServletResponse
     *
     *@return String the name of the jsp to call
     */
    public String process(HttpServletRequest request, HttpServletResponse response) throws Exception {
        

        HttpSession session = request.getSession();
        PrintHandler ph = new PrintHandler();

        TransactionDAO TXDAO = TransactionDAO.getInstance();
        TransLineDAO TLDAO = TransLineDAO.getInstance();
        
        //read in parameters and create sale RevSrc

       // ADD PHOTO SET   ph.makePhotoSet("Vacation", 30);
        
        
        
        String size = request.getParameter("size");
        String caption = request.getParameter("caption");
        String paper = request.getParameter("paper");
        String set = request.getParameter("set");
        int qty = Integer.parseInt(request.getParameter("qty"));
        double price = 0.3 * qty;
        
        
        String descr = "["+qty+"] '"+caption+"' @ "+size+" / "+paper;
        
        
        RevenueSourceBO rsBO = ph.addPrint(qty, descr, "00000111c5961b580000007f000002", size, paper, "Digital", price);
        
        
        //create & populate Transaction        
        TransactionBO TXBO = null;
        if(session.getAttribute("TX")==null)
        {
            TXBO = TXDAO.create();
            CustomerBO customer = (CustomerBO)session.getAttribute("customer");
            TXBO.setCustomerID(customer.getCustID());
            TXBO.setStoreID("0000011106a3cc370000007f000002");
            TXBO.setEmployeeID("0000011106a3cc390000007f000002");
            //set the date for the transaction
            Calendar cal = Calendar.getInstance();
            Date today = cal.getTime();
            SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
            String todayDate = fmt.format(today);
            TXBO.setTransDate(todayDate);
        }else{
            TXBO = (TransactionBO)session.getAttribute("TX");
        }
        
        
        TransLineBO TLBO = TLDAO.create();
        TLBO.setRevSrc(rsBO);
        TLBO.calcLineAmount();
        
        TXBO.addTxLine(TLBO);
        
        TXBO.calcSubTotal();
        TXBO.calcTax();        
        TXBO.calcTotal();
        TXBO.setTransStatus("Pending");
        
        request.setAttribute("TX", TXBO);
                
        session.setAttribute("TX", TXBO);        
        

        
        return "cart2.jsp";
    }//process
    
    
}//checkout class
